package com.luweijie.controller;


import cn.hutool.json.JSONString;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.JSONToken;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luweijie.entity.ShzwActivity;
import com.luweijie.entity.po.ShzwActivityPo;
import com.luweijie.entity.vo.Result;
import com.luweijie.excel.entity.OrderExcelBO;
import com.luweijie.excel.entity.util.ExcelUtil;
import com.luweijie.mapper.ShzwActivityMapper;
import com.luweijie.service.impl.ShzwActivityServiceImpl;
import com.luweijie.util.POJOConverter;
import com.luweijie.util.UserContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

import static com.luweijie.common.Constant.ActivityState.*;

/**
 * <p>
 * 优惠券信息及规则表 前端控制器
 * </p>
 *
 * @author luweijie
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/activity")
@Slf4j
public class ShzwActivityController {
    @Autowired
    private ShzwActivityServiceImpl shzwActivityServiceImpl;
    @Autowired
    ShzwActivityMapper shzwActivityMapper;


    @Resource(name = "pushThreadPoolExecutor")
    private Executor executor;

    @GetMapping("/normal/query")
    public Result Query(Page page) {
        IPage<ShzwActivityPo> query = shzwActivityServiceImpl.query(page, DISCOUNTS_ACTIVITY_NORMAL);
        return Result.success(query);
    }

    @GetMapping("/expiration/query")
    public Result expirationQuery(Page page) {
        IPage<ShzwActivityPo> query = shzwActivityServiceImpl.query(page, DISCOUNTS_ACTIVITY_EXPIRATION);
        return Result.success(query);
    }

    @GetMapping("/down/query")
    public Result query(Page page) {
        IPage<ShzwActivityPo> query = shzwActivityServiceImpl.query(page, DISCOUNTS_ACTIVITY_DOWN);
        return Result.success(query);
    }

    @PostMapping("/inster/activity")
    public Result insters(@RequestBody ShzwActivityPo shzwActivityPo) {
        log.info("新增的活动为：" + shzwActivityPo);
        System.out.println("ShzwActivityPo = " + shzwActivityPo);
        return Result.success(shzwActivityServiceImpl.add(shzwActivityPo));
    }

    @PutMapping(value = "/{id}")
    public Result update(@PathVariable int id, @RequestBody ShzwActivityPo shzwActivityPo) {
        log.info("更新的活动为：" + shzwActivityPo);
        System.out.println("ShzwActivityPo = " + shzwActivityPo);
        shzwActivityPo.setId(id);
        return Result.success(shzwActivityServiceImpl.updata(shzwActivityPo));
    }

    @GetMapping(value = "/queryAll")
    public Result queryAll(HttpServletResponse response) {
        List<OrderExcelBO> query = shzwActivityMapper.all();

        ExcelUtil.writeExcel(response, query, "luweijie", "luweijie", new OrderExcelBO());
        System.out.println("query = " + JSONObject.toJSONString(query));
        return Result.success();
    }

    @GetMapping(value = "/a")
    public Result queryAll(@RequestParam int id) {

for (int i = 0; i<=1000; i++){

    executor.execute(new Runnable() {
        @Override
        public void run() {
            AtomicInteger i = null;
            i.incrementAndGet();
        }
    });
}

return Result.success();
    }

}
